// pages/list/list.js
var app = getApp
Page({

  data: {
    imgUrls: [
    '../../image/3.png',
    '../../image/4.png',
    '../../image/5.png',
    '../../image/6.png'
    ],
    newsList: [],
    lastid:0,
    msg:"没有更多游记了",
    toastHidden: true,
    confirmHidden:true,
    isfrist:1,
    loadHidden:true,
    moreHidden:'none'
  },
  loadData: function (lastid){

    var limit = 2
    var that = this
    //显示加载中的提示
    this.setData({ loadHidden:false})
    //获取游记列表的数据
    wx.request({
      url: 'http://localhost/weicms/index.php?s=/addon/Cms/Cms/getList',
      data: {lastid:lastid,limit:limit},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        if(!res.data){
          that.setData({toastHidden:false})
          that.setData({ moreHidden: 'none'})
          return false
        }
        var len = res.data.length
        var oldLastid = lastid
        that.setData({lastid:res.data[len-1].id})

        if (oldLastid == 0) {
          wx.setStorageSync('CmsList', newData)
        }
        var dataArr = that.data.newsList
        var newData = dataArr.concat(res.data);

        that.setData({newsList: newData})
        that.setData({ moreHidden: '' })
        console.log('data from 服务器');
      },
      fail: function (res) {
        if (lastid == 0) {
          var newData = wx.getStorageSync('CmsList')
          if (!newData) {
            that.setData({ newsList: newData })
            that.setData({ moreHidden: '' })

            var len = newData.length
            that.setData({ lastid: newData[len - 1].id })
          }
          console.log('data from 本地');
        } else {
          that.setData({ toastHidden: false, moreHidden: 'none', msg: '当前网格异常，请稍后再试' })
        }
      },
      complete:function(){
        //显示加载中的提示
        that.setData({ loadHidden: true })
      }
    })
  },
  //加载更多内容
  loadMore: function (event) {
    var that = this
    var id =event.currentTarget.dataset.lastid
    var isfrist = event.currentTarget.dataset.isfrist
    wx.getNetworkType ({
      success: function(res) {
        var networkType = res.networkType

        if (networkType != 'wifi' && isfrist=='1'){
          that.setData({ confirmHidden: false })
        }
      }
    })
    this.setData({isfrist:0})
      this.loadData(id)
  },
  onLoad: function () {
    var that =this
    this.loadData(0);
  },
  toastChange: function () {
    this.setData({toastHidden:true})
  },
  modalChange:function(){
    this.setData({ confirmHidden: true })
  }

})